home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / gfx / show / GS510_data.lha / ghostscript / 5.10 / gs_res.ps < prev    next >
Text File  |  1997-12-28  |  19KB  |  666 lines

  1. %    Copyright (C) 1994, 1996, 1997 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of Aladdin Ghostscript.
  3. % Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  4. % or distributor accepts any responsibility for the consequences of using it,
  5. % or for whether it serves any particular purpose or works at all, unless he
  6. % or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  7. % License (the "License") for full details.
  8. % Every copy of Aladdin Ghostscript must include a copy of the License,
  9. % normally in a plain ASCII text file named PUBLIC.  The License grants you
  10. % the right to copy, modify and redistribute Aladdin Ghostscript, but only
  11. % under certain conditions described in the License.  Among other things, the
  12. % License requires that the copyright notice and this notice be preserved on
  13. % all copies.
  14.  
  15. % Initialization file for Level 2 resource machinery.
  16. % When this is run, systemdict is still writable,
  17. % but (almost) everything defined here goes into level2dict.
  18.  
  19. level2dict begin
  20.  
  21. (BEGIN RESOURCES) VMDEBUG
  22.  
  23. % We keep track of (global) instances with another entry in the resource
  24. % dictionary, an Instances dictionary.  For categories with implicit
  25. % instances, the values in Instances are the same as the keys;
  26. % for other categories, the values are [instance status size].
  27.  
  28. % Note that the dictionary that defines a resource category is stored
  29. % in global memory.  The PostScript manual says that each category must
  30. % manage global and local instances separately.  However, objects in
  31. % global memory other than systemdict can't reference objects in local memory.
  32. % This means that the resource category dictionary, which would otherwise be
  33. % the obvious place to keep track of the instances, can't be used to keep
  34. % track of local instances.  Instead, we define a dictionary in local VM
  35. % called localinstancedict, in which the key is the category name and
  36. % the value is the analogue of Instances for local instances.
  37.  
  38. % We don't currently implement automatic resource unloading.
  39. % When and if we do, it should be hooked to the garbage collector.
  40. % However, Ed Taft of Adobe says their interpreters don't implement this
  41. % either, so we aren't going to worry about it for a while.
  42.  
  43. currentglobal false setglobal systemdict begin
  44.   /localinstancedict 5 dict def
  45. end true setglobal
  46. /.emptydict 0 dict readonly def
  47. setglobal
  48.  
  49. % Resource category dictionaries have the following keys (those marked with
  50. % * are optional):
  51. %    Standard, defined in the Red Book:
  52. %        Category (name)
  53. %        *InstanceType (name)
  54. %        DefineResource
  55. %            <key> <instance> DefineResource <instance>
  56. %        UndefineResource
  57. %            <key> UndefineResource -
  58. %        FindResource
  59. %            <key> FindResource <instance>
  60. %        ResourceStatus
  61. %            <key> ResourceStatus <status> <size> true
  62. %            <key> ResourceStatus false
  63. %        ResourceForAll
  64. %            <template> <proc> <scratch> ResourceForAll -
  65. %        *ResourceFileName
  66. %            <key> <scratch> ResourceFileName <filename>
  67. %    Additional, specific to our implementation:
  68. %        Instances (dictionary)
  69. %        .LocalInstances
  70. %            - .LocalInstances <dict>
  71. %        .GetInstance
  72. %            <key> .GetInstance <instance> -true-
  73. %            <key> .GetInstance -false-
  74. %        .CheckResource
  75. %            <key> <value> .CheckResource <key> <value> <ok>
  76. %              (or may give an error if not OK)
  77. %        .DoLoadResource
  78. %            <key> .DoLoadResource - (may give an error)
  79. %        .LoadResource
  80. %            <key> .LoadResource - (may give an error)
  81. %        .ResourceFile
  82. %            <key> .ResourceFile <file> -true-
  83. %            <key> .ResourceFile <key> -false-
  84. % All the above procedures expect that the top dictionary on the d-stack
  85. % is the resource dictionary.
  86.  
  87. % Define enough of the Category category so we can define other categories.
  88. % The dictionary we're about to create will become the Category
  89. % category definition dictionary.
  90.  
  91. /.findcategory        % <name> .findcategory <category>
  92.  { { /Category findresource } stopped { pop stop } if
  93.  } bind def
  94.  
  95. /.resourceexec        % <key> /xxxResource .resourceexec -
  96.  {            %   (also pops the category from the dstack)
  97.    load stopped { Category end stop } if end
  98.  } bind def
  99.  
  100. 12 dict begin
  101.  
  102.         % Standard entries
  103.  
  104. /Category /Category def
  105. /InstanceType /dicttype def
  106.  
  107. /DefineResource
  108.     { .CheckResource
  109.        { dup /Category 3 index cvlit .growput
  110.         % We would like to make Category dictionaries read-only,
  111.         % and we used to do that here, but we can't do it,
  112.         % because we have to be able to replace the dummy, empty
  113.         % Instances dictionary with the real one later.
  114.          dup [ exch 0 -1 ] exch
  115.          Instances 4 2 roll put
  116.        }
  117.        { /defineresource load /typecheck signalerror
  118.        }
  119.       ifelse
  120.     } bind def
  121. /FindResource        % (redefined below)
  122.     { Instances exch get 0 get
  123.     } bind def
  124.  
  125.         % Additional entries
  126.  
  127. /Instances 25 dict def
  128. Instances /Category [currentdict 0 -1] put
  129.  
  130. /.LocalInstances 0 dict def
  131. /.GetInstance
  132.     { Instances exch .knownget
  133.     } bind def
  134. /.CheckResource
  135.     { dup gcheck currentglobal and
  136.        { /DefineResource /FindResource /ResourceForAll /ResourceStatus
  137.          /UndefineResource }
  138.        { 2 index exch known and }
  139.       forall
  140.       not { /defineresource load /invalidaccess signalerror } if
  141.       true
  142.     } bind def
  143.  
  144. Instances end begin    % for the base case of findresource
  145.  
  146. (END CATEGORY) VMDEBUG
  147.  
  148. % Define the resource operators.  We use the "stack protection" feature of
  149. % odef to make sure the stacks are restored properly on an error.
  150. % This requires that the operators not pop anything from the stack until
  151. % they have executed their logic successfully.  We can't make this
  152. % work for resourceforall, but we can make it work for the others.
  153.  
  154. mark
  155. /defineresource {    % <key> <instance> <category> defineresource <instance>
  156.     3 copy .findcategory dup begin
  157.     /InstanceType known {
  158.       dup type InstanceType ne {
  159.         dup type /packedarraytype eq InstanceType /arraytype eq and
  160.         not { /defineresource load /typecheck signalerror } if
  161.       } if
  162.     } if
  163.     /DefineResource .resourceexec
  164.     4 1 roll pop pop pop
  165. }
  166. /findresource {        % <key> <category> findresource <instance>
  167.     2 copy dup /Category eq
  168.       { pop //Category 0 get } { .findcategory } ifelse
  169.     begin
  170.     /FindResource .resourceexec exch pop exch pop
  171. }
  172. /resourceforall {    % <template> <proc> <scratch> <category> resourceforall -
  173.     .findcategory begin /ResourceForAll .resourceexec
  174. }
  175. /resourcestatus {    % <key> <category> resourcestatus <status> <size> true
  176.             % <key> <category> resourcestatus false
  177.     2 copy .findcategory begin /ResourceStatus .resourceexec
  178.      { 4 2 roll pop pop true } { pop pop false } ifelse
  179. }
  180. /undefineresource {    % <key> <category> undefineresource -
  181.     2 copy .findcategory begin /UndefineResource .resourceexec pop pop
  182. }
  183. end        % Instances of Category
  184. counttomark 2 idiv { bind odef } repeat pop
  185.  
  186. % Define the system parameters used for the Generic implementation of
  187. % ResourceFileName.
  188. systemdict begin
  189. currentdict /systemparams known not { /systemparams 10 dict readonly def } if
  190. systemparams
  191.   dup /FontResourceDir (/Resource/Font/) readonly .forceput
  192.   dup /GenericResourceDir (/Resource/) readonly .forceput
  193.   dup /GenericResourcePathSep (/) readonly .forceput
  194. pop
  195. end
  196.  
  197. % Define the generic algorithm for computing resource file names.
  198. /.rfnstring 100 string def
  199. /.genericrfn        % <key> <scratch> <prefix> .genericrfn <filename>
  200.  { 3 -1 roll //.rfnstring cvs concatstrings exch copy
  201.  } bind def
  202.  
  203. % Define the Generic category.
  204.  
  205. /Generic mark
  206.  
  207.         % Standard entries
  208.  
  209. % We're still running in Level 1 mode, so dictionaries won't expand.
  210. % Leave room for the /Category entry.
  211. /Category null
  212.  
  213. /DefineResource
  214.     { .CheckResource
  215.        { dup [ exch 0 -1 ]
  216.             % Stack: key value instance
  217.          currentglobal
  218.           { false setglobal 2 index UndefineResource    % remove local def if any
  219.         true setglobal
  220.         Instances dup //.emptydict eq
  221.          { pop 3 dict /Instances 1 index def
  222.          }
  223.         if
  224.           }
  225.           { .LocalInstances dup //.emptydict eq
  226.              { pop 3 dict localinstancedict Category 2 index put
  227.          }
  228.         if
  229.           }
  230.          ifelse
  231.             % Stack: key value instance instancedict
  232.          3 index 2 index .growput
  233.             % Now make the resource value read-only.
  234.          0 2 copy get { readonly } .internalstopped pop
  235.          dup 4 1 roll put exch pop exch pop
  236.        }
  237.        { /defineresource load /typecheck signalerror
  238.        }
  239.       ifelse
  240.     } bind
  241. /UndefineReso